package com.xyb.service;

import com.xyb.dao.BaseFileDao;
import com.xyb.entity.BaseFile;
import com.xyb.entity.FileInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class FileInfoService {
    @Resource
    private BaseFileDao baseFileDao;

    public FileInfo.InnerFile selectFileInfoByFileId(Long fileId) {
        BaseFile file = baseFileDao.selectBaseFileByFileId(fileId);
        if (file == null) {
            return new FileInfo.InnerFile();
        }
        FileInfo.InnerFile fileInfo = FileInfo.InnerFile.builder()
                .fileId(fileId)
                .bizId(file.getBizId())
                .fileName(file.getFileName())
                .prefixName(file.getPrefixName())
                .suffixName(file.getSuffixName())
                .fileType(file.getFileType())
                .fileSize(file.getFileSize())
                .urlName(file.getUrlName())
                .url(file.getUrl())
                .status(file.getStatus())
                .showOrder(file.getShowOrder())
                .createTime(file.getCreateTime())
                .build();
        return fileInfo;
    }

    public FileInfo.InnerBizFile selectFileInfoByBizId(Long bizId) {
        List<BaseFile> baseFiles = baseFileDao.selectBaseFileByBizId(bizId);
        FileInfo.InnerBizFile fileInfo = FileInfo.InnerBizFile.builder()
                .bizId(bizId)
                .build();
        if (baseFiles == null) {
            return fileInfo;
        }
        List<FileInfo.InnerFile> fileInfos = new ArrayList<>();
        baseFiles.forEach(file -> {
            FileInfo.InnerFile tmpFileInfo = FileInfo.InnerFile.builder()
                    .fileId(file.getFileId())
                    .bizId(file.getBizId())
                    .fileName(file.getFileName())
                    .prefixName(file.getPrefixName())
                    .suffixName(file.getSuffixName())
                    .fileType(file.getFileType())
                    .fileSize(file.getFileSize())
                    .urlName(file.getUrlName())
                    .url(file.getUrl())
                    .status(file.getStatus())
                    .showOrder(file.getShowOrder())
                    .createTime(file.getCreateTime())
                    .build();
            fileInfos.add(tmpFileInfo);
        });
        fileInfo.setFiles(fileInfos);
        return fileInfo;
    }
}
